package activities;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.maps.GeoPoint;
import com.searchparking.R;
import com.searchparking.BUS.DataSingleton;
import com.searchparking.BUS.ParseJsonData;
import com.searchparking.DTO.InfoObject;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ResultSearchActivity extends Activity implements OnClickListener {

	private TextView tvlocation;
	private static Spinner sp_distance;
	private RadioButton radioViewButton;;
	private ImageView imgview;
	public RadioGroup radioViewGroup;
	// Stores near by places
	String[] distance = null;
	Double latitude,longitude;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result_search);
		JSONObject data = DataSingleton.data;
        Intent intent = new Intent();
        latitude = intent.getDoubleExtra("latitude",0);
        longitude= intent.getDoubleExtra("longitude",0);
        
		ParseJsonData parseJon = new ParseJsonData();
		try {
			parseJon.parseData(data);
			LatLng current = new LatLng(latitude, longitude);
			// lay array list tra ve all vi tri hien tai 1 km ( duong chuot chay
			// )
			//parseJon.getListFromCurrent(1, current);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.d("Result", data.toString());
		initComponent();
		SetOnListener();

	}

	// Get and create control
	public void initComponent() {
		tvlocation = (TextView) findViewById(R.id.tvlocation_search);
		sp_distance = (Spinner) findViewById(R.id.sp_distance);
		imgview = (ImageView) findViewById(R.id.imgview);
		radioViewGroup = (RadioGroup) findViewById(R.id.radioview_search);
		// Set text for Location
		tvlocation.setText(MainActivity.address);
		// Array of place types
		distance = getResources().getStringArray(R.array.distance);

		// Creating an array adapter with an array of Place types
		// to populate the spinner
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, distance);

		// Setting adapter on Spinner to set place types
		sp_distance.setAdapter(adapter);

		// Radiobuton

	}

	// set onlistenerv for controll
	public void SetOnListener() {

		imgview.setOnClickListener(this);

	}

	// check radio button.

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.imgview:

			 int value_distance=  getValueSpinner(sp_distance);
			
			Toast.makeText(getApplicationContext(), toString().valueOf(value_distance),
					Toast.LENGTH_LONG).show();

			int selectedId = radioViewGroup.getCheckedRadioButtonId();
			radioViewButton = (RadioButton) findViewById(selectedId);
			 Log.d("Vi tri", latitude + " " + longitude );
			if (selectedId == R.id.rbt_map) {
				Toast.makeText(getApplicationContext(),
						radioViewButton.getText(), Toast.LENGTH_SHORT).show();
				Bundle bund  =  new Bundle();
				bund.putInt("distance", value_distance);
				bund.putDouble("latitude", this.latitude);
				bund.putDouble("longitude", this.longitude);
				Intent intent_map_view = new Intent(this, MapViewResult.class);
				intent_map_view.putExtras(bund);
				startActivity(intent_map_view);

			}
			if (selectedId == R.id.rbn_list) {
				Toast.makeText(getApplicationContext(),
						radioViewButton.getText(), Toast.LENGTH_SHORT).show();
				Intent intent_listview = new Intent(this, ListViewResult.class);
				Bundle bund = new Bundle();
				bund.putDouble("latitude",latitude);
				bund.putDouble("longitude",longitude);
				bund.putInt("distance", value_distance);
				intent_listview.putExtras(bund);
				startActivity(intent_listview);
			}
			break;

		}

	}

	// get value of spinner..
	public static int getValueSpinner (Spinner spinner)
	{	int index = 0;
		int value = spinner.getSelectedItemPosition();
		if (value==0)
		{
			index = 50;
		}
		else if(value==1)
		{
			index = 1;
		}
				else if(value==2){
					index = 3;
				}
					else if(value==3){
						index = 5;
					}
						else if(value==4){
							index = 10;
						}

		return index;
	}
}
